<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="${ctx}/css/main/base.css" rel="stylesheet" type="text/css"/>
<link href="${ctx}/css/main/main.css" rel="stylesheet" type="text/css"/>
</head>

<BODY style="background-color:#E3F1FD" onLoad="inntDepart();setdepart();">
<form method="post" name="myform">
  <table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="100" height="30"  nowrap="nowrap"><strong>选择人员：</strong></td>
      <td height="30" class="t_l"><input type="radio" name="depart"   onclick="inntDepart();" checked="checked"/>
        按部门选择
          <input type="radio" name="depart" onClick="inntRole();"/> 
          按职位选择
</td>
    </tr>
    <tr>
      <td width="100" height="30"  nowrap="nowrap"><strong>部门/职位：</strong></td>
      <td height="30" class="t_l"><select name="department" class="input01" style="display: block;" onChange="setdepart();">
        <option>所有部门</option>
      </select>
     <select name="role" class="input01" style="display: none;" onChange="setrole();">
        <option>所有职位</option>
      </select>
      </td>
    </tr>
    <tr>
	    <td width="100" height="40"  >
	    <strong>姓名查找：</strong>
	    </td>
	    <td class="t_l">
	    <input id="userName" name="baseUser.userName" type="text" class="inout01"  onkeyup="searchBySpell('userName');">
	       </td>
	       </tr>
  </table>
  <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="45%" height="25" align="center" background="images/maintlbg01.gif" class="tablezwz"><strong>可选</strong></td>
      <td height="25" align="center" bgcolor="#E3F1FD">&nbsp;</td>
      <td width="45%" height="25" align="center" background="images/maintlbg01.gif" class="tablezwz"><strong>已选</strong></td>
    </tr>
    <tr>
      <td width="45%" align="center" bgcolor="#E3F1FD">
		<select style="width:98%; height:200px; border:1px solid #898F94;" multiple name="list1" size="12" ondblclick="moveOption(document.myform.list1, document.myform.list2)">
	  </select></td>
      <td width="12%" align="center" bgcolor="#E3F1FD">
		<input type="button" class="but_bg" value="全选" onClick="moveAllOption(document.myform.list1, document.myform.list2)"><br><br>
	  <input type="button" class="but_bg" value="全清" onClick="moveAllOption(document.myform.list2, document.myform.list1)">	  </td>
      <td width="45%" align="center" bgcolor="#E3F1FD">
		<select style="width:98%; height:200px; border:1px solid #898F94;" multiple name="list2" size="12" ondblclick="moveOption(document.myform.list2, document.myform.list1)">
	  </select></td>
    </tr>
  </table>
  <table width="200" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <input type="hidden" name="usersId" value=""/>
      <input type="hidden" name="usersName" value=""/>
      <td height="50" align="center"><button class="but_bg" onClick="checkUsers();">确定</button></td>
      <td height="50" align="center"><button class="but_bg" onClick="javascript:window.close();">取消</button></td>
    </tr>
  </table>
</form>
<script type="text/javascript" src="${ctx}/scripts/commons/jquery-1.2.3.pack.js"></script>
<script language="JavaScript">
var count = 0;

function inntDepart(){
	var department = document.getElementById("department");
	var role = document.getElementById("role");
	if(role.style.display == "block"){
		role.style.display = "none";
		department.style.display = "block";
		$.ajax({
			type:"GET",
			url:"${ctx}/user/user!inntDepartment.action",
			data:"time=" + new Date().getTime(),
			dataType:"json",
			success:function (json){
				var departs = json.departments;
				
				if(departs != null){	
					for(var i = 0;i < departs.length;i++){
							department.options[i] = new Option(departs[i].deptName,departs[i].deptId);
					}
				}
			},
			error:function (){
				alert("系统异常!");
			}
		});
	}else{
		department.style.display = "block";
		$.ajax({
			type:"GET",
			url:"${ctx}/user/user!inntDepartment.action",
			data:"time=" + new Date().getTime(),
			dataType:"json",
			success:function (json){
				var departs = json.departments;
					for(var i = 0;i < departs.length;i++){
							department.options[i] = new Option( departs[i].deptName,departs[i].deptId);
					}
			},
			error:function (){
				alert("系统异常!");
			}
		});
	
	}
}
function inntRole(){
	var department = document.getElementById("department");
	var role = document.getElementById("role");
	if(department.style.display == "block"){
		department.style.display = "none";
		role.style.display = "block";
		$.ajax({
			type:"GET",
			url:"${ctx}/user/user!inntRole.action",
			data:"time=" + new Date().getTime(),
			dataType:"json",
			success:function (json){
				var roles = json.baseroles;
					for(var i = 1;i < roles.length;i++){
						role.options[i] = new Option(roles[i].roleName,roles[i].roleId);
					}
			},
			error:function (){
				alert("系统异常!");
			}
		});
	}else{
		role.style.display = "block";
		$.ajax({
			type:"GET",
			url:"${ctx}/index/chooseUser!inntRole.action",
			data:"time=" + new Date().getTime(),
			dataType:"json",
			success:function (json){
				var roles = json.baseroles;
					for(var i = 1;i < roles.length;i++){
						role.options[i] = new Option(roles[i].roleName,roles[i].roleId);
					}
			},
			error:function (){
				alert("系统异常!");
			}
		});
	
	}
}

function setdepart(){
   var departId = document.getElementById("department").value;
	var list1 = document.getElementById("list1");
	$.ajax({
			type:"GET",
			url:"${ctx}/user/user!findDepartment.action",
			data:"departId=" + departId +"&flag=1"+ "&time=" + new Date().getTime(),
			dataType:"json",
			success:function (json){
				var users1 = json.users1;
				cleacSelect('list1');
				for(var i = 0;i < users1.length;i++){
					list1.options[i] = new Option(users1[i].userName,users1[i].userId);
				}
				
			},
			error:function (){
				alert("系统异常!");
			}
		});
}

function cleacSelect(objID){
	var selectObj = document.getElementById(objID);
	var length = selectObj.options.length;
	for(var i = 0;i < length;i++){
		selectObj.options[0] = null;
	}
}

function setrole(){
   var roleId = document.getElementById("role").value;
	var list1 = document.getElementById("list1");
	$.ajax({
			type:"GET",
			url:"${ctx}/user/user!findDepartment.action",
			data:"roleId=" + roleId +"&flag=2"+ "&time=" + new Date().getTime(),
			dataType:"json",
			success:function (json){
				var users = json.users;
				cleacSelect('list1');
					for(var i = 0;i < users.length;i++){
						list1.options[i] = new Option(users[i].userName,users[i].userId);
					}
			},
			error:function (){
				alert("系统异常!");
			}
		});

}


function moveOption(e1, e2){
	try{
		for(var i=0;i<e1.options.length;i++){
			if(e1.options[i].selected){
				var e = e1.options[i];
				e2.options.add(new Option(e.text, e.value));
				e1.remove(i);
				i=i-1
			}
		}
	}
	catch(e){}
}

function moveAllOption(e1, e2){
	try{
		for(var i=0;i<e1.options.length;i++){
				var e = e1.options[i];
				e2.options.add(new Option(e.text, e.value));
				e1.remove(i);
				i=i-1
		}
	}
	catch(e){}

}

function checkUsers(){

var id=document.getElementById('list2');
	   // var values='';
	    var array=new Array();	
	    for(var i=0;i<id.length;i++)
	    { 
	       array[i]=new Array();
	       array[i][0]=id[i].text;
	       array[i][1]=id[i].value;
	    }
	    window.returnValue=array;
	    window.close();
	    
}

function searchBySpell(inputId){
	var spell = document.getElementById(inputId).value;
	if(spell != "" && spell != null){
					$.ajax({
					url:"${ctx}/user/user!allspellList.action",
					data:"spell=" + spell + "&time=" + new Date().getTime(),
					type:"post",
					dataType:"json",
					success:function(json){
						var users = json.users;
						if(users.length > 0){
						 cleacSelect('list1');
							var list = document.getElementById('list1');
							 for(var i = 0;i < users.length;i++){
								list.options[i] = new Option(users[i].userName,users[i].userId);
						  		 }
						}else{
						 cleacSelect('list1');
						}
						
					},
					error:function(){
						alert("系统异常！");
					}
				});
			}else{
			 cleacSelect('list1');
			}
}
</script>
</BODY>
</HTML>
